<?php
namespace app\api\controller\Meta;
use support\exception\ValidateException;
use app\api\model\Meta\Ue as UeModel;
use app\api\controller\CommonController;
use think\facade\Db;

class UeController extends CommonController{



	/**
	 *@description 查看详情
	 *@buildcode(true)
	*/
	function detail(){
		$postField = 'ue_id';
		$data = $this->request->only(explode(',',$postField),'post');

		if(empty($data['ue_id'])){
			throw new ValidateException ('编号不能为空');
		}
		$field = 'ue_id,meta_id,name,ttsType_id,sttType_id,chat_id,role_id,username,password,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
		$query = UeModel::field($field);

		$query = $query->where('ue_id',$data['ue_id']);

		$res = $query->findOrEmpty();

		if($res->isEmpty()){
			throw new ValidateException ('信息不存在');
		}

		return json(['status'=>200,'data'=>$res]);
	}


	/**
	 *@description 用户登录
	 *@buildcode(false)
	*/
	public function login(){
		$postField = 'username,password';
		$data = $this->request->only(explode(',',$postField),'post');
		if(empty($data['username'])) throw new ValidateException ('用户名不能为空');
		if(empty($data['password'])) throw new ValidateException ('密码不能为空');

		$res = Db::name('ue')
								->alias('meta')
								->field('meta.name as meta_name,meta.model_id,meta.meta_id,model.name as model_name,meta.wakeup_keyword, model.sex as model_sex, meta.status as status,stt.type as stt_type, tts.type as tts_type  , chat.type as chat_type')  // 选择需要查询的字段，可以使用别名来区分不同表中相同字段名
								->join('cd_typestt stt', 'meta.sttType_id = stt.typestt_id', 'LEFT')
								->join('cd_typetts tts', 'meta.ttsType_id = tts.typetts_id', 'LEFT')
								->join('cd_chat chat', 'meta.chat_id = chat.chat_id', 'LEFT')
								->join('cd_ue_model model', 'meta.model_id = model.ue_model_id', 'LEFT')
								->where('username', '=', $data['username'])
								->where('password', '=', md5($data['password'].config('my.password_secrect')))
								->find();

		if($res == null){
			throw new ValidateException ('账号或密码错误');
		}
		return json(['status'=>200,'data'=>$res,'token'=>$this->setToken(json_encode($res))]);
	}


}